package com.hcrazy.manchester.app.controllers;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.RadioTag;
import com.hcrazy.manchester.app.services.RadioTagService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

/**
 * 电台分类管理
 * @author Hsmart
 *
 */
@Controller
@RequestMapping("/admin/radio")
public class RadioTagController {
	
	@Autowired
	private RadioTagService radioTagService;
	
	@RequestMapping(value="/radioTag",method=RequestMethod.GET)
	public void radioTag(){
		
	}
	
	@RequestMapping(value="/addRadioTag",method=RequestMethod.GET)
	public void addRadioTag(@RequestParam int parentId,Model model){
		addChildSort(parentId,model);
		
	}
	
	/**
	 * 跳转到添加子分类页面
	 * 初始化数据
	 * @param parentId
	 * @param model
	 */
	public void addChildSort(int parentId,Model model){
		//根据父分类id获取父分类对象
		try {
			RadioTag parentSort = new RadioTag();
			if(parentId == 0){
				parentSort.setTagId(0);
				parentSort.setTagName("无");
			}else{
				parentSort = radioTagService.getSortById(parentId);
			}
			model.addAttribute("parentSort",parentSort);
			RadioTag radioTag = new RadioTag();
			model.addAttribute("radioTag",radioTag);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
	}
	
	@RequestMapping(value="/editRadioTag",method=RequestMethod.GET)
	public String editRadioTag(@RequestParam int sortId,Model model){
		editChildSort(sortId,model);
		return "/admin/radio/addRadioTag";
	}
	
	/**
	 * 跳转到编辑分类页面
	 * @param sortId
	 * @param model
	 * @return
	 */
	public void editChildSort(int sortId,Model model){
		
		try {
			RadioTag radioTag = radioTagService.getSortById(sortId);
			addChildSort(radioTag.getParentId(),model);
			
			model.addAttribute("radioTag",radioTag);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
	}
	
	@RequestMapping(value="/delRadioTag",method=RequestMethod.GET)
	public String delRadioTag(@RequestParam int sortId,Model model){
		delSort(sortId,model);
		return "/admin/radio/radioTag";
	}
	
    
	/**
	 * 删除分类
	 * @param sortId
	 * @return
	 */
	public void delSort(int sortId,Model model){
		try {
			String msg = radioTagService.delRadioTag(sortId);
			if(null != msg){
				model.addAttribute("message", msg);
			}
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	
	@RequestMapping(value="/radioTag",method=RequestMethod.POST)
    public String addOrUpdateNews(@ModelAttribute("form") RadioTag sort,Model model){
		try {
			sort.setModuleType(Constant.NEWS_TYPE_NEWS);
			radioTagService.addOrUpdateRadioTag(sort);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "/admin/radio/radioTag";
	}
	
	
	/**
	 * 获取全部分类（树状结构）
	 * @return
	 */
	@RequestMapping(value="/tags",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<RadioTag> getAllReportSorts(){
		
		try {
			return radioTagService.getAllSorts(Constant.RADIO_TAG_RADIO,0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	
}
